{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T14:48:20.777281Z",
     "start_time": "2018-02-02T14:48:20.774099Z"
    }
   },
   "source": [
    "## 问题： 求y = w^2 - 10*w +25的最小值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T15:32:46.734043Z",
     "start_time": "2018-02-02T15:32:46.728847Z"
    }
   },
   "source": [
    "## tensorflow求导的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T00:14:34.672698Z",
     "start_time": "2018-02-03T00:14:34.669185Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T00:14:35.058496Z",
     "start_time": "2018-02-03T00:14:35.048692Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "w = tf.Variable(0, dtype=tf.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T00:14:35.722204Z",
     "start_time": "2018-02-03T00:14:35.394395Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n",
      "100.0\n"
     ]
    }
   ],
   "source": [
    "cost = tf.add(tf.add(w**2, tf.multiply(-10., w)), 100.0)\n",
    "train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)\n",
    "\n",
    "init = tf.global_variables_initializer()\n",
    "session = tf.Session()\n",
    "session.run(init)\n",
    "print(session.run(w))\n",
    "print(session.run(cost))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T00:14:36.098734Z",
     "start_time": "2018-02-03T00:14:35.835352Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.99999\n",
      "75.0\n"
     ]
    }
   ],
   "source": [
    "for i in range(1000):\n",
    "    session.run(train)\n",
    "print(session.run(w))  # 1000次迭代后w的取值\n",
    "print(session.run(cost))  # 算出此时cost的值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:03:07.337489Z",
     "start_time": "2018-02-02T23:03:07.331152Z"
    }
   },
   "source": [
    "## 注入data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T00:15:16.381152Z",
     "start_time": "2018-02-03T00:15:16.367101Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "w = tf.Variable(0, dtype=tf.float32)\n",
    "coeficients = np.array([[1.], [-10.], [26.]])\n",
    "x = tf.placeholder(tf.float32, [3, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T00:15:17.202205Z",
     "start_time": "2018-02-03T00:15:16.735151Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n",
      "0.1\n"
     ]
    }
   ],
   "source": [
    "cost = x[0][0] * w **2 + x[1][0] * w + x[2][0]\n",
    "train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)\n",
    "init = tf.global_variables_initializer()\n",
    "session = tf.Session()\n",
    "session.run(init)\n",
    "print(session.run(w))\n",
    "\n",
    "session.run(train, feed_dict={x: coeficients})\n",
    "print(session.run(w))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T00:15:39.936003Z",
     "start_time": "2018-02-03T00:15:39.509393Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4.99999\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "for i in range(1000):\n",
    "    session.run(train, feed_dict={x: coeficients})\n",
    "print(session.run(w))\n",
    "print(session.run(cost, feed_dict={x:coeficients}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## mxnet求导的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:15:58.619042Z",
     "start_time": "2018-02-02T23:15:56.875236Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import mxnet.ndarray as nd\n",
    "import mxnet.autograd as ag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:15:58.628791Z",
     "start_time": "2018-02-02T23:15:58.620809Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "x = nd.array([0])\n",
    "x.attach_grad()  # 当进行求导的时候，我们需要一个地方来存x的导数，这个可以通过NDArray的方法attach_grad()来要求系统申请对应的空间。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:15:58.635639Z",
     "start_time": "2018-02-02T23:15:58.630816Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def SGD(x, lr):\n",
    "    x[:] = x - lr * x.grad\n",
    "    \n",
    "def f(x):\n",
    "    return x ** 2 - 10 * x + 25"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:16:00.857445Z",
     "start_time": "2018-02-02T23:16:00.327727Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "for i in range(1000):\n",
    "    with ag.record(): # 默认情况下, MXNet不会自动记录和构建用于求导的计算图，我们需要使用autograd里的record()函数来显式的要求MXNet记录我们需要求导的程序。\n",
    "        y = f(x)\n",
    "    y.backward()\n",
    "    SGD(x, 0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-02T23:18:13.066475Z",
     "start_time": "2018-02-02T23:18:13.057235Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "[ 4.99998856]\n",
      "<NDArray 1 @cpu(0)>\n",
      "\n",
      "[ -2.28881836e-05]\n",
      "<NDArray 1 @cpu(0)>\n",
      "\n",
      "[ 0.]\n",
      "<NDArray 1 @cpu(0)>\n"
     ]
    }
   ],
   "source": [
    "print(x)  # 1000次迭代后x的取值\n",
    "print(x.grad)\n",
    "print(f(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:30:06.402454Z",
     "start_time": "2018-02-03T15:30:06.381525Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9848785337744963\n"
     ]
    }
   ],
   "source": [
    "r = np.random.rand() # [0, 1]均匀分布\n",
    "beta = 1-10**(- r - 1)\n",
    "print(beta)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## numpy 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:30:28.196654Z",
     "start_time": "2018-02-03T15:30:28.188233Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(20).reshape((5,4))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:30:43.444174Z",
     "start_time": "2018-02-03T15:30:43.440052Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [12 13 14 15]]\n"
     ]
    }
   ],
   "source": [
    "print(a[[0, 1, 3], :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:32:20.065772Z",
     "start_time": "2018-02-03T15:32:20.058926Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3],\n",
       "       [ 4,  5,  6,  7],\n",
       "       [12, 13, 14, 15]])"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[[0, 1, 3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:36:25.504573Z",
     "start_time": "2018-02-03T15:36:25.500559Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "row_idx = np.array([0, 1, 3])\n",
    "col_idx = np.array([0, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:36:35.249778Z",
     "start_time": "2018-02-03T15:36:35.239582Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0],\n",
       "       [1],\n",
       "       [3]])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row_idx[:, None]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-02-03T15:39:00.418151Z",
     "start_time": "2018-02-03T15:39:00.410765Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  2],\n",
       "       [ 4,  6],\n",
       "       [12, 14]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[np.ix_([0,1,3], [0,2])]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
